from appium.webdriver.common.appiumby import AppiumBy

from 水质.utils.Logger import logger
from 水质.utils.click import clickText, clickId, swipeUp, swipeLeft, wait_Id, wait_clickId


def swipeSmartTable(driver):
    el = driver.find_elements(AppiumBy.ID, "smart_table")
    if len(el) > 0:
        el = el[0]
        swipeUp(driver, el)
        swipeUp(driver, el)
        swipeLeft(driver, el)


def clickReload(driver):
    el = driver.find_element(AppiumBy.ID, "tv_reload")
    if el != None:
        el.click()
        logger.log("点击面积率定时间")
        rv_selection = wait_Id(driver, "rvSelection")
        child_elements = rv_selection.find_elements(AppiumBy.XPATH, ".//*")
        if len(child_elements) <= 0:
            logger.error("元素长度不够，率定时间列表为空")
        assert len(child_elements) > 0

        child_elements[0].click()
        logger.log("选择第一个时间")
        checkSmartTableAppreace(driver)


def checkSmartTableAppreace(driver):
    wait_Id(driver, "smart_table")
    swipeSmartTable(driver)


def export_excel(driver):
    # 导出excel
    wait_clickId(driver, "imageView19")
    wait_clickId(driver, "md_button_positive")
    wait_clickId(driver, "md_button_negative")


def checkTableArea(driver):
    clickId(driver, "iv_menu")
    clickText(driver, "断面水位面积查算报表")
    swipeSmartTable(driver)
    clickReload(driver)
    clickId(driver, "textView407")  # 水位范围
    et_min = wait_Id(driver, "et_min")
    et_min.send_keys("1")
    et_max = wait_Id(driver, "et_max")
    et_max.send_keys("2")
    clickId(driver, "tv_confirm")
    export_excel(driver)
    wait_clickId(driver, "iv_h")
    export_excel(driver)
    wait_clickId(driver, "iv_h")

